//
//  SelfDiagnosisViewController.m
//  MobileChecker
//
//  Created by Lymons on 15-1-8.
//  Copyright (c) 2015年 Lymons. All rights reserved.
//

#import "SelfDiagnosisViewController.h"

const CGFloat kFadeAnimationDuration = 0.2f;

@interface SelfDiagnosisViewController ()

@end

@implementation SelfDiagnosisViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initializeView];
    [self bindViewModel];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initializeView
{
    self.tapSingleGesture = [[UITapGestureRecognizer alloc] init];
    self.tapSingleGesture.delegate = self;
    self.tapSingleGesture.numberOfTapsRequired = 1;
    [self.viewForSingleTap addGestureRecognizer:self.tapSingleGesture];
    
    self.tapDoubleGesture = [[UITapGestureRecognizer alloc] init];
    self.tapDoubleGesture.delegate = self;
    self.tapDoubleGesture.numberOfTapsRequired = 2;
    self.tapDoubleGesture.numberOfTouchesRequired = 1;
    [self.viewForDoubleTap addGestureRecognizer:self.tapDoubleGesture];
    if (self.viewForDoubleTap == self.viewForSingleTap) {
        [self.tapSingleGesture requireGestureRecognizerToFail:self.tapDoubleGesture];
    }
}

- (void)handleSingleTap:(id)x
{
    
}

- (void)handleDoubleTap:(id)x
{
    
}

- (UIView *)viewForDoubleTap
{
    return self.view;
}

- (UIView *)viewForSingleTap
{
    return self.view;
}

#pragma mark -

- (void)bindViewModel {

    [[self.tapSingleGesture rac_gestureSignal] subscribeNext:^(id x) {
        [self handleSingleTap:x];
    }];
    
    [[self.tapDoubleGesture rac_gestureSignal] subscribeNext:^(id x) {
        [self handleDoubleTap:x];
    }];
}

#pragma mark -
#pragma mark View

- (void)showFullscreenModalViewWithView:(UIView *)view animated:(BOOL)animated completion:(void(^)(void))completion
{
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
    
    if (animated) {
        view.alpha = 0.0f;
    }
    
    [self.navigationController.view addSubview:view];
    
    if (animated) {
        [UIView animateWithDuration:kFadeAnimationDuration
                         animations:^{
                             view.alpha = 1.0;
                         }
                         completion:^(BOOL finished) {
                             if (completion) {
                                 completion();
                             }
                         }];
    } else {
        if (completion) {
            completion();
        }
    }
}

- (void)dismissFullscreenModalViewWithView:(UIView *)view animated:(BOOL)animated completion:(void(^)(void))completion
{
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
    
    if (animated) {
        [UIView animateWithDuration:kFadeAnimationDuration
                         animations:^{
                             view.alpha = 0.0;
                         }
                         completion:^(BOOL finished) {
                             [view removeFromSuperview];
                             if (completion) {
                                 completion();
                             }
                         }];
    } else {
        [view removeFromSuperview];
        if (completion) {
            completion();
        }
    }
}

#pragma mark - UIGestureRecognizerDelegate
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if([touch.view isKindOfClass:[UIButton class]])
    {
        return NO;
    }
    return YES;
}

@end
